package com.henme.crm.service.admin.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.henme.crm.dao.admin.UserMapper;
import com.henme.crm.entity.admin.User;
import com.henme.crm.service.admin.UserService;
import com.henme.crm.util.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl  implements UserService{
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserMapper userMapper;

    public void insertUser(User  user){
        userMapper.insertOne(user);
    }
    public void updateUser(User  user){
        userMapper.updateOne(user);
    }
    public void deleteUser(long id){
        userMapper.deleteOne(id);
    }

    public User selectUser(long id){
        return 	userMapper.selectOne(id);
    }

    public Page<User> getUserList(Page<User> page){
        List<User> list = userMapper.findUserPage(page);
        page.setResult(list);
        return page;
    }

    @Override
    public void saveUserRole(Long userid, String[] roleids) {
        // 第一步删除用户角色对应
        userMapper.deleteUserRole(userid);
        if (roleids.length > 0) {
            // 第二不批量保存 user_id role_id对应关系
            List<Map<String, Object>> l = Lists.newArrayList();
            for (String m : roleids) {
                Map<String, Object> param = Maps.newHashMap();
                param.put("role_id", m);
                param.put("user_id", userid);
                l.add(param);
            }
            userMapper.insertUserRoleBatch(l);
        }

    }
    @Override
    public void markLocked(String[] ids) {
        userMapper.markLocked(ids);

    }

    @Override
    public void markNotLocked(String[] ids) {
        userMapper.markNotLocked(ids);
    }
    @Override
    public void markLockedbyUserid(long id) {
        userMapper.markLockedbyUserid(id);

    }
    @Override
    public void markNotLockedbyUserid(long id) {
        userMapper.markNotLockedbyUserid(id);
    }
    @Override
    public User queryUniqueByUsername(String username) {
        return userMapper.queryUniqueByUsername(username);
    }
}